package com.tr.rentcar.model.dao;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import com.tr.rentcar.model.GlobalParamHandler;
import com.tr.rentcar.model.bean.CarAttr;
import com.tr.rentcar.model.jdbc.MySqlDb;
import com.tr.rentcar.model.jdbc.ResultHandler;

/**
 * 车辆属性Dao
 * @author dell
 *
 */
public class CarAttrDao implements ResultHandler<CarAttr> {

	MySqlDb db=MySqlDb.getInstance();
	@Override
	public CarAttr doHander(Map<String, Object> row) {
		CarAttr ca=new CarAttr();
		ca.setCaId(Integer.parseInt(row.get("ca_id")+""));
		ca.setCid(Integer.parseInt(row.get("cid")+""));
		ca.setCsId(Integer.parseInt(row.get("cs_id")+""));
		ca.setCsName(row.get("cs_name")+"");
		ca.setCaName(row.get("ca_name")+"");
		ca.setAvValue(row.get("av_value")+"");
		return ca;
	}
	/**
	 * 按车辆的类型名称查找
	 */
	/**
	 *按车辆id查询该车辆所有属性 
	 * @return
	 * @throws SQLException
	 */
	public List<CarAttr> getAllById(Integer cid) throws SQLException{
		String sql="select * from car_attr_view where cid=?";
		return db.executeQuery(sql ,new GlobalParamHandler(cid), this);
	}
	/**
	 * 添加一条记录
	 */
	public boolean insert(String name,String csid) throws SQLException{
		String sql="insert into car_attr(ca_name,cs_id) values(?,?)";
		return db.executeUpdate(sql, new GlobalParamHandler(name,csid))>0;
	}
	public boolean insert2(String value) throws SQLException{
		String sql="insert into car_attr_val(av_value) values(?)";
		return db.executeUpdate(sql, new GlobalParamHandler(value))>0;
	}
	
	/**
	 * 查询所有
	 */
	public List<CarAttr> getAll() throws SQLException{
		String sql="select * from car_style ";
		return db.executeQuery(sql, this);
	}
	/**
	 * 按照id删除
	 */
	public boolean delete(Integer Id) throws SQLException{
		String sql="delete from car_attr where ca_id=?";
		return db.executeUpdate(sql, new GlobalParamHandler(Id))>0;
	}
	/**
	 * 修改attr值
	 */
	public boolean update(String name,Integer csId) throws SQLException{
		String sql="update car_style set cs_name=? where cs_id=?";
		return db.executeUpdate(sql, new GlobalParamHandler(name,csId))>0;
	}
	/**
	 * 修改attrval
	 */
	public boolean update2(String name,String caid) throws SQLException{
		String sql="update car_attr set ca_name=? where ca_id=?";
		return db.executeUpdate(sql, new GlobalParamHandler(name,caid))>0;
	}
	public boolean update3(String type,String caid) throws SQLException{
		String sql="update car_attr_val set av_value=? where ca_id=?";
		return db.executeUpdate(sql, new GlobalParamHandler(type,caid))>0;
	}
	
	
}
